package com.dongjiantong.farmrecords.model;

import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import lombok.Data;

import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@Table(name = "farmrecords_harmlesses_no_center")
public class FarmRecordsHarmlesses extends BaseEntity implements Serializable {
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    /**
     * 1.集中处理无害化 2.非集中处理无害化
     */
    private Integer type;

    private String number;
    /**
     * 关联牛档案id
     */
    private Long cow_record_id;
    /**
     * 牛号
     */
    private String cow_number;

    private Long farm_id;

    private String farm_name;

    private Long stable_id;

    private String stable_number;

    private Date date;

    private Integer area;

    private Integer breed_type;

    private Integer breed_second_type;

    private Integer breed_third_type;

    private Integer unit;

    private Integer quantity;

    private Integer death_reason;

    private Integer treatment;

    @JsonIgnore
    private String ear_tag_number_json;
    @Transient
    private List<String> ear_tag_number;

    /**
     * 视频快照
     */
    private String video_snap;

    /**
     * 1表示生产死淘过来的，提交后不用做生产死淘
     * 2的话用户自己做的，需要死淘单
     */
    private Integer is_from;

    private String operator;

    private String remark;

    private Integer province_id;

    private Integer city_id;

    private Integer region_id;

    private Integer street_id;

    private Integer village_id;

    private Integer status;

    private Integer delete_flag;

    private Date created_at;

    private Date updated_at;

    @Transient
    private List<String> picList;

    @Transient
    private Map<String,Object> videoList;

    @Transient
    private String video_toObject;

    public void setVideo_toObject (String video_toObject) {
        this.video_toObject = video_toObject;

        if (Strings.isNullOrEmpty(video_toObject)) {
            this.videoList = null;
        } else {
            try {
                this.videoList = objectMapper.readValue(video_toObject,new TypeReference<Map<String,Object>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setVideoList (Map<String,Object> videoList) {
        this.videoList = videoList;

        if (videoList == null || videoList.isEmpty()) {
            this.video_toObject = null;
        } else {
            try {
                this.video_toObject = objectMapper.writeValueAsString(videoList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setEar_tag_number_json(String ear_tag_number_json) {
        this.ear_tag_number_json = ear_tag_number_json;

        if (Strings.isNullOrEmpty(ear_tag_number_json)) {
            this.ear_tag_number = null;
        } else {
            try {
                this.ear_tag_number = objectMapper.readValue(ear_tag_number_json,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setEar_tag_number (List<String> ear_tag_number) {
        this.ear_tag_number = ear_tag_number;

        if (ear_tag_number == null || ear_tag_number.isEmpty()) {
            this.ear_tag_number_json = "[]";
        } else {
            try {
                this.ear_tag_number_json = objectMapper.writeValueAsString(ear_tag_number);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

}